package J.算法.买卖股票问题;

public class _122买卖股票的最佳时机II {


    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0] ;
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][i]+prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] - prices[i]);

        }
        return dp[n-1][0];
    }

    /**
     * 状态压缩
     * @param prices
     * @return
     */
    public int maxProfit1(int[] prices) {
        int n = prices.length;
//        int[][] dp = new int[n][2];
//        dp[0][0] = 0;
//        dp[0][1] = -prices[0] ;
        int dp_0 = 0;
        int dp_1= -prices[0] ;
         for (int i = 1; i < n; i++) {
             int temp = dp_0;
             dp_0 = Math.max(dp_0,dp_1+prices[i]);
             dp_1 = Math.max(dp_1,temp - prices[i]);

        }
        return dp_0;
    }
}
